﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Brain;
using Brain.Logic2D.PathFinding.Tiled;

namespace Demos
{
    public class PathFinding2DTestTiled : DemoScreen
    {
        public Map Map;

        public Node StartNode;
        public Node EndNode;

        public Camera2D Camera;

        public PathFinder Finder;

        float BlockSize = 50;
        public PathFinding2DTestTiled()
        {
            Map = new Map(10, 10);

            Camera = new Camera2D(this);
            
            Engine.Instance.CameraManager.Camera2D = Camera;

            float half = BlockSize / 2f;
            for (ushort x = 0; x < 10; x++)
            {
                Node xnode = new Node();
                xnode.Rectangle = new Rectangle((int)(x * BlockSize), 0, (int)BlockSize, (int)BlockSize);
                Map.SubmitNode(xnode, x, 0);
                // Start = 4:0
                if (x == 4)
                {
                    StartNode = xnode;
                }

                for (ushort y = 1; y < 10; y++)
                {
                    Vector2 v = new Vector2(x * BlockSize, y * BlockSize);
                    Node node = new Node();
                    node.Rectangle = new Rectangle((int)v.X, (int)v.Y, (int)BlockSize, (int)BlockSize);
                    Map.SubmitNode(node, x, y);
                    // 1:4 até 8:4
                    if (y == 4)
                    {
                        if (x != 0 && x != 9)
                        {
                            node.Blocked = true;
                        }
                    }
                    // 8:5 até 8:7
                    // 1:5 até 1:7
                    else if (x == 8 || x == 1)
                    {
                        if (y == 5 || y == 6 || y == 7)
                        {
                            node.Blocked = true;
                        }
                    }
                    // 2:7 até 7:7
                    else if (y == 7)
                    {
                        if (x == 2 || x == 3 || x == 6 || x == 7)
                        {
                            node.Blocked = true;
                        }
                    }
                    // End = 4:5
                    else if (y == 5 && x == 4)
                    {
                        EndNode = node;
                    }
                }
            }

            Map.FinishedMaking();

            Finder = new PathFinder(this.Map);
            Finder.CalculateAllAtOnce = false;
            Finder.Search(this.StartNode, this.EndNode);
            Finder.TimeStep = 0; // Update Every frame

            for (int x = 0; x < Map.Width; x++)
            {
                for (int y = 0; y < Map.Height; y++)
                {
                    Node node = Map.Nodes[x, y];
                    Sprite sprite = new Sprite();
                    sprite.Rendering = SpriteRendering.Rendered;
                    sprite.BoundsRectangle = node.Rectangle;
                    if (node.Blocked)
                    {
                        sprite.BoundsRectangleColor = Color.Red;
                    }
                    else if (node == StartNode)
                    {
                        sprite.BoundsRectangleColor = Color.Yellow;
                    }
                    else if (node == EndNode)
                    {
                        sprite.BoundsRectangleColor = Color.Green;
                    }
                    else
                    {
                        sprite.BoundsRectangleColor = Color.LightBlue;
                    }
                }
            }

            Finder.FoundPath += new SetVoid(Finder_FoundPath);
        }

        void Finder_FoundPath()
        {
            float under4 = BlockSize / 4f;
            for (int i = 0; i < Finder.WayList.Count; i++)
            {
                Node node = Finder.WayList[i];
                Sprite sprite = new Sprite();
                sprite.Rendering = SpriteRendering.Rendered;
                sprite.BoundsRectangle =
                    new Rectangle((int)(node.Rectangle.X + under4),
                        (int)(node.Rectangle.Y + under4), 
                        node.Rectangle.Width / 2,
                        node.Rectangle.Height / 2);
                sprite.BoundsRectangleColor = Color.Blue;
            }
        }
        public override void Update()
        {
            base.Update();

            Finder.Update();

            float Speed = 5;

            if (Engine.Instance.InputManager.IsKeyDown(Keys.W) ||
                Engine.Instance.InputManager.IsAnyPlayerButtonDown(Buttons.LeftThumbstickUp))
            {
                Camera.Position += new Vector2(0, Speed);
            }
            if (Engine.Instance.InputManager.IsKeyDown(Keys.S) ||
                Engine.Instance.InputManager.IsAnyPlayerButtonDown(Buttons.LeftThumbstickDown))
            {
                Camera.Position -= new Vector2(0, Speed);
            }
            if (Engine.Instance.InputManager.IsKeyDown(Keys.A) ||
                Engine.Instance.InputManager.IsAnyPlayerButtonDown(Buttons.LeftThumbstickLeft))
            {
                Camera.Position += new Vector2(Speed, 0);
            }
            if (Engine.Instance.InputManager.IsKeyDown(Keys.D) ||
                Engine.Instance.InputManager.IsAnyPlayerButtonDown(Buttons.LeftThumbstickRight))
            {
                Camera.Position -= new Vector2(Speed, 0);
            }
        }

        public override void Draw()
        {
            base.Draw();




        }
    }
}
